//
//  SetupPasswordView.m
//  PigMarket
//
//  Created by Felix on 2019/3/15.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SetupPasswordView.h"

@interface SetupPasswordView ()
/**
 提示label
 */
@property (nonatomic , strong) UILabel *tipsLab;

/**
 输入框背景
 */
@property (nonatomic , strong) UIView *inputBgView;



/**
 passwordSecurityBtn
 */
@property (nonatomic , strong) UIButton *passwordSecurityBtn;

/**
 confirmPasswordSecutityBtn
 */
@property (nonatomic , strong) UIButton *confirmPasswordSecutityBtn;

/**
 灰线
 */
@property (nonatomic , strong) UIView *lineView;

;

@end

@implementation SetupPasswordView

#pragma mark - init method

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
        [self initAction];
    }
    return self;
}


- (void)initView {
    // 添加子控件
    [self addSubview:self.tipsLab];
    [self addSubview:self.inputBgView];
    [self.inputBgView addSubview:self.lineView];
    [self.inputBgView addSubview:self.passwordField];
    [self.inputBgView addSubview:self.confirmPasswordField];
    [self addSubview:self.saveBtn];
    [self.inputBgView addSubview:self.passwordSecurityBtn];
    [self.inputBgView addSubview:self.confirmPasswordSecutityBtn];

}


- (void)initLayout {
    // 布局子控件
    [self.tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(6);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(SCREEN_WIDTH - 40);
    }];
    [self.inputBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.tipsLab.mas_bottom).offset(6);
        make.height.mas_equalTo(109);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.centerY.mas_equalTo(self.inputBgView.mas_centerY);
        make.height.mas_equalTo(1);
    }];
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self.lineView.mas_top);
        make.right.mas_equalTo(-40);
    }];
    
    [self.confirmPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.bottom.mas_equalTo(self.inputBgView.mas_bottom);
        make.right.mas_equalTo(-40);
    }];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.inputBgView.mas_bottom).offset(60);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(48);
        make.bottom.mas_equalTo(0);
    }];
    [self.passwordSecurityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.passwordField.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.confirmPasswordSecutityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.confirmPasswordField.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}


- (void)initAction {
    [self.passwordSecurityBtn addTarget:self action:@selector(passwordSecurityClick) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmPasswordSecutityBtn addTarget:self action:@selector(confirmPasswordSecurityClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)passwordSecurityClick {
    
    self.passwordField.secureTextEntry = !self.passwordField.secureTextEntry;
    self.passwordSecurityBtn.selected = !self.passwordSecurityBtn.selected;
}


- (void)confirmPasswordSecurityClick {
    self.confirmPasswordField.secureTextEntry = !self.confirmPasswordField.secureTextEntry;
    
    self.confirmPasswordSecutityBtn.selected = !self.confirmPasswordSecutityBtn.selected;
    
}


#pragma mark - getter and setter


- (UILabel *)tipsLab {
    if (!_tipsLab) {
        _tipsLab = [[UILabel alloc] init];
        _tipsLab.textColor = [UIColor tipsGrayColor];
        _tipsLab.font = [UIFont middleFont];
        _tipsLab.text = @"密码只支持数字，字母及下划线，6-16个字符";
    }
    return _tipsLab;
}


- (UIView *)inputBgView {
    if (!_inputBgView) {
        _inputBgView = [[UIView alloc] init];
        _inputBgView.backgroundColor = [UIColor whiteColor];
    }
    return _inputBgView;
}


- (UITextField *)passwordField {
    if (!_passwordField) {
        _passwordField = [[UITextField alloc] init];
        _passwordField.placeholder = @"请输入密码";
        _passwordField.secureTextEntry = YES;
    }
    return _passwordField;
}


- (UITextField *)confirmPasswordField {
    if (!_confirmPasswordField) {
        _confirmPasswordField = [[UITextField alloc] init];
        _confirmPasswordField.placeholder = @"请再次输入密码";
        _confirmPasswordField.secureTextEntry = YES;
    }
    return _confirmPasswordField;
}


- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _lineView;
}


- (UIButton *)saveBtn {
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
    }
    return _saveBtn;
}


- (UIButton *)passwordSecurityBtn {
    if (!_passwordSecurityBtn) {
        _passwordSecurityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_passwordSecurityBtn setImage:[UIImage imageNamed:@"login_hidden"] forState:UIControlStateNormal];
        [_passwordSecurityBtn setImage:[UIImage imageNamed:@"login_show"] forState:UIControlStateSelected];
    }
    return _passwordSecurityBtn;
}


- (UIButton *)confirmPasswordSecutityBtn {
    if (!_confirmPasswordSecutityBtn) {
        _confirmPasswordSecutityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmPasswordSecutityBtn setImage:[UIImage imageNamed:@"login_show"] forState:UIControlStateSelected];
        [_confirmPasswordSecutityBtn setImage:[UIImage imageNamed:@"login_hidden"] forState:UIControlStateNormal];
        
    }
    return _confirmPasswordSecutityBtn;
}
@end
